package touchtrack.controller;

import java.awt.Image;
import java.util.LinkedList;
import java.util.List;

import touchtrack.exception.IllegalDuplicationException;
import touchtrack.exception.ItemNotFoundException;
import touchtrack.model.World;
import touchtrack.model.artist.Artist;
import touchtrack.model.track.LocalTrack;
import touchtrack.model.track.Track;
import touchtrack.model.track.TrackFactory;
import touchtrack.model.track.TrackInterface;

/**
 * TrackController
 * 
 * @author	MuMe09 team 6
 * 			http://mumetech.wordpress.com/
 */
public class TrackController
{
	/**
	 * Constructor.
	 * 
	 * @param 	world
	 */
	public TrackController(World world)
	{
		this.world = world;
	}

	/***************************************************************************
	 * 								Creation
	 ***************************************************************************/
	
	/**
	 * Creates a LocalTrack.
	 * 
	 * @param 	String artist
	 * @param 	String title
	 * @param	Image image
	 * @param	String summary
	 * @param	String mp3path
	 * @throws 	IllegalDuplicationException
	 */
	public void createLocalTrack(String artist, String title, Image image, String summary, String mp3path) 
		throws IllegalDuplicationException
	{
		this.createTrack(artist, title, image, "local", summary, mp3path);
	}
	
	/**
	 * Creates a ExternalTrack.
	 * 
	 * @param 	String artist
	 * @param 	String title
	 * @param	Image image
	 * @param	String summary
	 * @throws 	IllegalDuplicationException
	 */
	public void createExternalTrack(String artist, String title, Image image, String summary) 
		throws IllegalDuplicationException
	{
		this.createTrack(artist, title, image, "external", summary, null);
	}
	
	/**
	 * Creates a Track.
	 * 
	 * @param 	String artist
	 * @param 	String title
	 * @param	Image image
	 * @param 	String type ("external" or "local")
	 * @param	String summary
	 * @param	String mp3path (null if external path)
	 * @throws 	IllegalDuplicationException
	 */
	public void createTrack(String artist, String title, Image image, String type, String summary, String mp3path) 
		throws IllegalDuplicationException
	{
		// Find or create & add artist first.
		Artist a;
		try {
			a = this.world.getArtistDatabase().find(artist);
		} catch (ItemNotFoundException e) {
			a = new Artist(artist);
			this.world.getArtistDatabase().add(a);
		}
		
		// Check type & create Track.
		Track t = TrackFactory.createTrack(a, title, image, type, mp3path);
		if (summary != null)
			t.setSummary(summary);
		else
			t.setSummary("");
		
		// Add to database.
		this.world.getTrackDatabase().add(t);
	}
	
	/***************************************************************************
	 * 								Tracks
	 ***************************************************************************/
	
	/**
	 * @return	List<TrackInterface>
	 */
	public List<TrackInterface> getTracks()
	{
		LinkedList<TrackInterface> tracks = new LinkedList<TrackInterface>();
		tracks.addAll( world.getTrackDatabase().fetchAll() );
		return tracks;
	}
	
	/**
	 * Plays a given track.
	 * 
	 * @param 	String track
	 * @effect	Plays the track. (duuh)
	 */
	public void playTrack(String track)
	{
		try {
			Track t = world.getTrackDatabase().find(track);
			if ( t instanceof LocalTrack )
				((LocalTrack)t).play();
		} catch (ItemNotFoundException e) {
			// ignore... lolzyeawhatever
		}
	}
	
	/**
	 * @param 	String artist
	 * @param 	String trackTitle
	 * @return	boolean
	 */
	public boolean containsTrack(String artist, String trackTitle)
	{
		return this.containsTrack(artist + " - " + trackTitle);
	}
	
	/**
	 * @param 	String track
	 * @return	boolean
	 */
	public boolean containsTrack(String track)
	{
		try {
			world.getTrackDatabase().find(track);
		} catch (ItemNotFoundException e) {
			return false;
		}
		
		return true;
	}
	
	/**
	 * @param 	String trackA
	 * @param 	String trackB
	 * @param	float similarity
	 * @throws 	ItemNotFoundException
	 */
	public void makeSimilar(String trackA, String trackB, float similarity) 
		throws ItemNotFoundException
	{
		Track a = world.getTrackDatabase().find(trackA);
		Track b = world.getTrackDatabase().find(trackB);
		
		try {
			a.addSimilarTrack(b, similarity);
		} catch (IllegalDuplicationException e) {
			// already added: ignore exception
		}
	}
	
	/**
	 * @param 	String track
	 * @throws 	ItemNotFoundException
	 */
	public void removeTrack(String track) 
		throws ItemNotFoundException
	{
		Track t = world.getTrackDatabase().find(track);
		t.remove();
		world.getTrackDatabase().remove(t);
	}

	/***************************************************************************
	 * 							World reference
	 ***************************************************************************/
	
	private World world;
	
}